package chpt12;

/**
 * 连接并访问数据库
 * PreparedStatement
 */

import java.sql.*;
public class PreparedStatementTest {
	public static void main(String[] args) {
		String url = "jdbc:mysql://127.0.0.1:3306/demoDB?serverTimezone=Asia/Chongqing";
		String userName = "root";
		String pwd = "root123";
		String sql = "select * from students where gpa>=? and name like ?";
		try (Connection con = DriverManager.getConnection(
				url, userName, pwd);
			PreparedStatement pstmt = con.prepareStatement(sql)){
			pstmt.setDouble(1, 2.5);
			pstmt.setString(2, "陈%");
			ResultSet rs = pstmt.executeQuery();
			while (rs.next())
				System.out.println("学号=" + rs.getString(2) + 
						"; 姓名=" + rs.getString(3) + 
						"; GPA=" + rs.getDouble(4));
		} catch (SQLException e){
			e.printStackTrace();
		}
	}
}
